package com.blt.govdata.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class TreeUtil {

    Logger logger = LoggerFactory.getLogger(TreeUtil.class);

    public static void main(String[] args) {
        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("id", "1");
        map1.put("text", "1");
        map1.put("pId", "0");
        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("id", "2");
        map2.put("text", "1");
        map2.put("pId", "1");
        List<Map<String, Object>> list = new ArrayList<>();
        list.add(map1);
        list.add(map2);
        TreeUtil t = new TreeUtil();

        t.help(list);

    }

    public Object creatTreeData(List<Map<String, Object>> menuList) {
        try {
            List<Map<String, Object>> nodeList = new ArrayList<Map<String, Object>>();
            for (Map<String, Object> node1 : menuList) {
                boolean mark = false;
                for (Map<String, Object> node2 : menuList) {
                    if (node2.get("isParent") != null && "1".equals(node2.get("isParent"))) {
                        node2.put("children", new ArrayList<Map<String, Object>>());
                    }
                    if (node1.get("pId") != null && node1.get("pId").equals(node2.get("id"))) {
                        mark = true;
                        if (node2.get("children") == null)
                            node2.put("children", new ArrayList<Map<String, Object>>());
                        @SuppressWarnings("unchecked")
                        List<Map<String, Object>> nodes = (List<Map<String, Object>>) node2.get("children");
                        nodes.add(node1);
                        break;
                    }
                }
                if (!mark) {
                    nodeList.add(node1);
                }
            }
            return nodeList;
        } catch (Exception e) {
            this.logger.error(e.getMessage());
            return null;
        }
    }

    public Object help(List<Map<String, Object>> results) {

        final Map<Object, Map<String, Object>> id_map = new LinkedHashMap<>(results.size());
        final Map<Object, List<Object>> id_nodes = new LinkedHashMap<>(results.size());
        final Map<Object, List<Object>> id_parents = new LinkedHashMap<>(results.size());
        for (Map<String, Object> map : results) {
            final Object id = map.get("id");
            id_map.put(id, map);
            final List<Object> nodes = new ArrayList<>();
            id_nodes.put(id, nodes);
            map.put("nodes", nodes);

//            final Object parent_id = map.containsKey("parent_id") ? map.get("parent_id") : "";
            final List<Object> parents = new ArrayList<>();
            id_parents.put(id, parents);
            map.put("parents", parents);
        }

        for (Iterator<Map<String, Object>> ite = results.iterator(); ite.hasNext(); ) {
            final Map<String, Object> map = ite.next();
            final Object id = map.get("id");
            Object parentid = map.get("pId");
            if (parentid == null || "".equals(parentid)) {
                continue;
            }
            Map<String, Object> parent_map = id_map.get(parentid);
            if (parent_map != null) {
                final List<Object> nodes = id_nodes.get(parentid);
                nodes.add(map);

                final List<Object> parents = id_parents.get(id);
                parents.add(parentid);
                while ((parentid = parent_map.get("pId")) != null && !"".equals(parentid) && (parent_map = id_map.get(parentid)) != null) {
                    parents.add(parentid);
                }
                ite.remove();
            }
        }

        for (Map<String, Object> map : id_map.values()) {
//            final List<Object> parents = id_parents.get(map.get("id"));
//            final String parents_str = parents.toString().replace("[", "").replace("]", "").replaceAll("\\s+","");
//            map.put("parents", parents_str);
            if (map.containsKey("parents"))
                map.remove("parents");

            final List<Object> nodes = id_nodes.get(map.get("id"));
            if (nodes.size() == 0) {
                map.remove("nodes");
            }
        }
        return results;
    }

}
